/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-26
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.displaytag.util.ParamEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.util.WebUtils;

import com.springyouner.lottery.framework.UserSession;
import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.LotteryWinner;
import com.springyounger.lottery.dal.dataobject.User;
import com.springyounger.lottery.service.user.PhaseService;

/**
 * TODO Comment of HomeController
 * 
 * @author springyounger
 */
public class WinnerController implements Controller {

	private static final Logger logger = LoggerFactory
			.getLogger(WinnerController.class);
	private PhaseService phaseService;

	@SuppressWarnings("unchecked")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String yourPhaseId = (String) WebUtils.getSessionAttribute(request,
				"phaseId");
		UserSession userSession = (UserSession) WebUtils.getSessionAttribute(
				request, "userSession");
		User user = userSession.getUser();
		int phaseId = Integer.parseInt(yourPhaseId);
		int userid = user.getUserid();
		// 页数的参数名
		String pageIndexName = new ParamEncoder("row")
				.encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE);

		// 每页显示的条数
		int pageSize = 10;
		// 当前页
		int pageIndex = LotteryUtil
				.isEmpty(request.getParameter(pageIndexName)) ? 0 : (Integer
				.parseInt(request.getParameter(pageIndexName)) - 1);
		// 总记录
		int resultSize = phaseService.getAllWinnerCountById(userid, phaseId);
		List<LotteryWinner> winnerList = new ArrayList<LotteryWinner>();
		winnerList = phaseService.getWinnerById(userid, phaseId, pageIndex,
				pageSize);

		if (null == winnerList || winnerList.size() == 0) {
			logger.info("奖品列表为空");
		}
		Map model = new HashMap();
		model.put("winnerList", winnerList);
		model.put("resultSize", resultSize);
		return new ModelAndView("user/myWinner", model);
	}

	/**
	 * @param phaseService
	 *            the phaseService to set
	 */
	public void setPhaseService(PhaseService phaseService) {
		this.phaseService = phaseService;
	}
}
